package com.zhang.network.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.network.mapper.FriendMapper;
import com.zhang.network.mapper.NetworkMapper;
import com.zhang.network.mapper.NetworkUserMapper;
import com.zhang.network.model.Friend;
import com.zhang.network.model.NetworkUser;
import com.zhang.network.model.User;
import com.zhang.network.model.network.NetworkUserAddDTO;
import com.zhang.network.model.network.NetworkUserVo;
import com.zhang.network.model.network.SecondNetworkQueryDTO;
import com.zhang.network.service.NetworkUserService;
import com.zhang.network.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangshuaiyin
 * @since 2021-04-03
 */
@Service
public class NetworkUserServiceImpl extends ServiceImpl<NetworkUserMapper, NetworkUser> implements NetworkUserService {

    final UserService userService;
    @Resource
    FriendMapper friendMapper;
    @Resource
    NetworkMapper networkMapper;

    @Autowired
    public NetworkUserServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<NetworkUserVo> getUsersByNetworkId(Integer networkId) {
        List<NetworkUserVo> networkUserVoList = new ArrayList<>();
        List<NetworkUser> networkUsers = this.list(new QueryWrapper<NetworkUser>().eq("network_id", networkId));
        List<Integer> userIds = networkUsers.stream().map(NetworkUser::getUserId).collect(Collectors.toList());
        NetworkUser admin = networkUsers.stream().filter(item -> item.getIsAdmin().equals(1)).collect(Collectors.toList()).get(0);
        List<User> users = userService.listByIds(userIds);
        NetworkUserVo vo = null;
        for (User user : users) {
            vo = new NetworkUserVo();
            BeanUtils.copyProperties(user, vo);
            if (user.getId().equals(admin.getUserId())) {
                vo.setIsAdmin(1);
            } else {
                vo.setIsAdmin(0);
            }
            networkUserVoList.add(vo);
        }
        return networkUserVoList;
    }

/*    @Override
    public Page<NetworkUserVo> pageUsersByNetwork(NetworkUserQueryDTO dto) {
        Page<NetworkUser> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<NetworkUser> networkUsers = this.list(new QueryWrapper<NetworkUser>().eq("network_id", dto.getNetworkId()));
        Page<NetworkUser> networkUsersPage = baseMapper.selectPage(page, new QueryWrapper<NetworkUser>().eq("network_id", dto.getNetworkId()));

        List<NetworkUserVo> networkUserVoList = new ArrayList<>();
        List<Integer> userIds = networkUsers.stream().map(NetworkUser::getUserId).collect(Collectors.toList());
        NetworkUser admin = networkUsers.stream().filter(item -> item.getIsAdmin().equals(1)).collect(Collectors.toList()).get(0);
        List<User> users = userService.listByIds(userIds);
        NetworkUserVo vo = null;
        for (User user : users) {
            vo = new NetworkUserVo();
            BeanUtils.copyProperties(user, vo);
            if (user.getId().equals(admin.getUserId())) {
                vo.setIsAdmin(1);
            } else {
                vo.setIsAdmin(0);
            }
            networkUserVoList.add(vo);
        }
        Page<NetworkUserVo> networkUserVoPage = new Page<>();
        BeanUtils.copyProperties(networkUsersPage, networkUserVoPage);
        networkUserVoPage.setRecords(networkUserVoList);
        return networkUserVoPage;
    }*/

    @Override
    public List<User> getUsersNotInNetworkById(Integer networkId) {
        // 找到当前人脉圈中的好友列表
        List<NetworkUser> networkUsers = baseMapper.selectList(new QueryWrapper<NetworkUser>().eq("network_id", networkId));
        List<Integer> networkUserIds = networkUsers.stream().map(NetworkUser::getUserId).collect(Collectors.toList());
        List<User> userOfNetworkList = userService.listByIds(networkUserIds);

        // 找到人脉圈管理员，然后找到管理员的所有好友列表
        NetworkUser adminNetworkUser = networkUsers.stream().filter(item -> item.getIsAdmin() == 1).collect(Collectors.toList()).get(0);
        List<Friend> friendList = friendMapper.selectList(new QueryWrapper<Friend>().eq("user_id", adminNetworkUser.getUserId()));
        List<Integer> friendIds = friendList.stream().map(Friend::getFriendId).collect(Collectors.toList());
        List<User> userOfFriendList = userService.listByIds(friendIds);

        // 所有好友列表 减去在人脉圈的好友列表
        return userOfFriendList.stream().filter(item -> !userOfNetworkList.contains(item)).collect(Collectors.toList());
    }

    /**
     * 我和好友共同人脉圈中的成员-我已有的好友
     *
     * @param dto
     * @return
     */
    @Override
    public Page<User> getSecondNetwork(SecondNetworkQueryDTO dto) {
        List<NetworkUser> networkUserList1 = baseMapper.selectList(new QueryWrapper<NetworkUser>().eq("user_id", dto.getUserId()).eq("is_admin", 0));
        List<NetworkUser> networkUserList2 = baseMapper.selectList(new QueryWrapper<NetworkUser>().eq("user_id", dto.getFriendId()));
        List<NetworkUser> networkUsers = networkUserList2.stream().filter(networkUserList1::contains).collect(Collectors.toList());
        List<Integer> userIdList = networkUsers.stream().map(NetworkUser::getUserId).collect(Collectors.toList());
        List<Integer> friendIdList = friendMapper.selectList(new QueryWrapper<Friend>().eq("user_id", dto.getUserId())).stream().map(Friend::getFriendId).collect(Collectors.toList());
        List<Integer> collect = userIdList.stream().filter(item -> !friendIdList.contains(item)).collect(Collectors.toList());
         return userService.page(new Page<>(dto.getPageNum(), dto.getPageSize()), new QueryWrapper<User>().in("id", collect));
    }

    @Override
    public void insertNetworkUsers(NetworkUserAddDTO dto) {
        NetworkUser networkUser = new NetworkUser();
        networkUser.setNetworkId(dto.getNetworkId());
        networkUser.setUserId(dto.getUserId());
        networkUser.setIsAdmin(1);
        baseMapper.insert(networkUser);
        for (Integer friendId : dto.getFriendIds()) {
            networkUser = new NetworkUser();
            networkUser.setNetworkId(dto.getNetworkId());
            networkUser.setUserId(friendId);
            networkUser.setIsAdmin(0);
            baseMapper.insert(networkUser);
        }
    }

    @Override
    public void deleteUserOfNetwork(NetworkUser networkUser) {
        Map<String, Object> map = new HashMap<>();
        map.put("network_id", networkUser.getNetworkId());
        map.put("user_id", networkUser.getUserId());
        baseMapper.deleteByMap(map);
    }
}
